package com.gurid.service.edu.controller.admin;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.gurid.common.base.result.R;
import com.gurid.service.edu.entity.Teacher;
import com.gurid.service.edu.entity.vo.TeacherQueryVo;
import com.gurid.service.edu.feign.OssFileService;
import com.gurid.service.edu.service.TeacherService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.tags.Tag;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * <p>
 * 讲师 前端控制器
 * </p>
 *
 * @author Gurid
 * @since 2024-04-29
 */
@Tag(name = "TeacherController", description = "讲师管理")
//@CrossOrigin //允许跨域
@RestController
@RequestMapping("/admin/edu/teacher")
//@RefreshScope //动态更新配置中心数据，配合配置文件的配置使用，缺一不可
public class TeacherController {

    @Autowired
    private TeacherService teacherService;

    @Autowired
    private OssFileService ossFileService; //通过Feign远程调用Oss服务的方法

    @Operation(summary = "讲师列表", description = "获取所有讲师")
    @GetMapping("list")
    public R listAll() {
        List<Teacher> list = teacherService.list();
        return R.ok().data("items", list);
    }

    @Operation(summary = "删除讲师", description = "根据id删除讲师，逻辑删除")
    @DeleteMapping("remove/{id}")
    public R removeById(@Parameter(description = "讲师Id", required = true) @PathVariable String id){
        //先删除头像
        teacherService.removeAvatarById(id);
        boolean isRemoved = teacherService.removeById(id);
        return (isRemoved == true ? R.ok().message("删除成功！") : R.error().message("数据不存在"));
    }

    @Operation(summary = "讲师分页列表")
    @GetMapping("list/{page}/{limit}")
    public R listPage(@Parameter(description = "当前页码", required = true) @PathVariable Long page,
                      @Parameter(description = "每页的记录数", required = true) @PathVariable Long limit,
                      @Parameter(description = "讲师列表查询对象") TeacherQueryVo teacherQueryVo){
        Page<Teacher> pageParam = new Page<>(page, limit);
        IPage<Teacher> pageModel = teacherService.selectPage(pageParam, teacherQueryVo);
        List<Teacher> records = pageModel.getRecords();
        long total = pageModel.getTotal();
        return R.ok().data("total", total).data("rows", records);
    }

    @Operation(summary = "新增讲师")
    @PostMapping("save")
    public R save(@Parameter(description = "讲师对象", required = true) @RequestBody Teacher teacher){
        teacherService.save(teacher);
        return R.ok().message("保存成功");
    }

    @Operation(summary = "更新讲师")
    @PutMapping("update")
    public R updateById(@Parameter(description = "讲师对象", required = true) @RequestBody Teacher teacher){
        Boolean result = teacherService.updateById(teacher);
        if (result){
            return R.ok().message("更新成功");
        } else {
            return R.error().message("数据不存在");
        }
    }

    @Operation(summary = "讲师信息", description = "根据id获取讲师信息")
    @GetMapping("get/{id}")
    public R getById(@Parameter(description = "讲师对象") @PathVariable String id){
        Teacher teacher = teacherService.getById(id);
        if(teacher != null) {
            return R.ok().data("item", teacher);
        } else { //可能刚获取到teacher就被删除了，导致对象数据为空
            return R.error().message("数据不存在");
        }
    }

    @Operation(summary = "讲师列表", description = "根据关键字查询讲师列表")
    @GetMapping("list/name/{key}")
    public R selectNameListByKey(
            @Parameter(description = "关键字", required = true)
            @PathVariable String key) {
        List<Map<String, Object>> nameList = teacherService.selectNameList(key);
        return R.ok().data("nameList", nameList);
    }

    @Operation(summary = "测试服务调用")
    @GetMapping("test")
    public R test(){
        ossFileService.test();
        System.out.println("edu执行成功");
        return R.ok();
    }

    @Operation(summary = "测试并发")
    @GetMapping("test_concurrent")
    public R testConcurrent(){
        System.out.println("并发执行成功");
        return R.ok();
    }
}
